package com.jack.leetcode.eachproblem_2020;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhang_j on 2020/11/14
 */
public class 数组的相对排序 {

    public static void main(String[] args) {
        int[] a = {2,3,1,3,2,4,6,7,9,2,19};
        int[] b = {2,1,4,3,9,6};
        relativeSortArray(a, b);
    }

    public static int[] relativeSortArray(int[] arr1, int[] arr2) {
        if(arr2 == null){
            return arr1;
        }
        int upper = 0;
        for(int i : arr1){
            upper = Math.max(upper, i);
        }
        int[] freq = new int[upper + 1];
        for(int i : arr1){
            freq[i]++;
        }
        int[] result = new int[arr1.length];
        int index = 0;
        for(int i = 0;i < arr2.length;i++){
            for(int j = 0;j < freq[arr2[i]];j++){
                result[index++] = arr2[i];
            }
            freq[arr2[i]] = 0;
        }
        for(int i = 0;i <= upper;i++){
            for(int j = 0;j < freq[i];j++){
                result[index++] = i;
            }
        }
        return result;
    }

}
